<?php

namespace Common\Model;

/**
 * HotelCouponModel.class.php实例模型
 * 
 * @author wenhui
 * @date 2017-011-09
 */
class UserCouponModel extends BaseModel {

    protected $trueTableName = 'bk_user_coupon';

    protected function _before_insert(&$data, $options) {
    }

    protected function _before_update(&$data, $options) {
    }

    /**
     * 添加数据
     * @modify wenhui 2017-01-02    创建
     * @param  int   $userId    id
     * @param  string   $couponId   id
     * @param  string   $hotelId   id
     */
    public function addUserCoupons($userId, $couponId, $hotelId) {
        $data = array(
            'userlogin_id' => $userId,
            'coupon_id' => $couponId,
            'hotelentity_id' => $hotelId,
        );

        return $this->add($data);
    }

    /**
     * 获取数据
     * @modify wenhui 2017-01-02    创建
     * @param  int   $userId    微预定用户id
     * @param  int   $couponId    优惠券id
     * @return data
     */
    public function getUserCoupon($userId, $couponId = null) {
        $option = [
            'userlogin_id'=> $userId,
            'isdelete'=> NOT_DELETE,
        ];

        if (!is_null($couponId)) {
            $option['coupon_id'] = $couponId;
        }

        $data = $this->where($option)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 获取数据
     * @modify wenhui 2017-01-02    创建
     * @param  int   $couponId    优惠券id
     * @return data
     */
    public function getCouponTotal($couponId, $type = null) {
        $option['coupon_id'] = $couponId;

        if (!is_null($type)) {
            $option['type'] = $type;
        }

        $field = 'count(*) as coupontotal';
        $data = $this->field($field)->where($option)->select();

        return empty($data) ? 0 : $data;
    }

    /**
     * 保存领取码
     * @modify wenhui 2018-01-02
     * @param int $id  id
     * @param string $no  领取码
     * @return type
     */
    public function saveUserGetNo($id, $no) {
        $data = array();

        $options['id'] = $id;
        $data['no'] = $no;

        return $this->where($options)->save($data) === false ? false : true;
    }

     /**
     * 
     * @modify wenhui 2018-01-02
     * @param int $id  id
     * @param string $no  领取码
     * @return type
     */
    public function getUserCouponByGetNo($id) {
        $options['id'] = $id;
        //$options['isdelete'] = NOT_DELETE;

        $data = $this->where($options)->find();
        return  empty($data) ? array() : $data;
    }

    /**
     * 保存数据
     * @modify wenhui 2018-01-02
     * @param int $id  id
     * @param int $orderId  订单id
     * @return type
     */
    public function saveUserCouponOrderId($id, $orderId = null, $type = null) {
        $data = array();

        $options['id'] = $id;

        if (!is_null($orderId)) {
            $data['orderinfo_id'] = $orderId;
        }
        if (!is_null($type)) {
            $data['type'] = $type;
        }

        return $this->where($options)->save($data) === false ? false : true;
    }

    /**
     * 保存数据
     * @modify wenhui 2018-01-02
     * @param int $id  id
     * @param int $type
     * @return type
     */
    public function saveUserCouponType($couponId, $type) {
        $options['coupon_id'] = $couponId;
        $options['type'] = USER_COUPON_OUTDATED;
        $data['type'] = $type;

        return $this->where($options)->save($data) === false ? false : true;
    }

    /**
     * 
     * @modify wenhui 2018-01-02
     * @param int $id  orderId
     */
    public function getUserCouponByOrderId($orderId) {
        $options['orderinfo_id'] = $orderId;
        $options['isdelete'] = NOT_DELETE;

        $data = $this->where($options)->find();
        return  empty($data) ? array() : $data;
    }

    /**
     * 获取数据
     * @modify wenhui 2017-01-02    创建
     * @param  int   $couponId    优惠券id
     * @return data
     */
    public function getUserCouponByCouponId($couponId) {
        $option = [
            'coupon_id'=> $couponId,
            'isdelete'=> NOT_DELETE,
        ];

        $data = $this->where($option)->select();
        return empty($data) ? array() : $data;
    }

     /**
     * 获取数据
     * @modify wenhui 2017-01-02    创建
     * @param  int   $hotel    客栈id
     * @return data
     */
    /*public function getUserCouponByHotel($hotel) {
        $option = [
            'hotelentity_id'=> $hotel,
            'isdelete'=> NOT_DELETE,
        ];

        $data = $this->where($option)->select();
        return empty($data) ? array() : $data;
    }*/
}
